<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
  <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
</head>
<body>
<!--<input type="hidden" name="rowId">-->
<div class="layuimini-container">

  <div class="layuimini-main">
    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
      </div>
    </script>

<!--    <table class="layui-hide" id="currentTableId" ></table>-->
    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    <input type="hidden" name="hiddenFieldForTable" id="hiddenFieldForTable">
    <script type="text/html" id="currentTableBar">
      <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" type="button">删除</a>
    </script>

  </div>
</div>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../api/common.js" charset="utf-8"></script>
<script>
  layui.use(['form', 'treeTable'], function () {
    var $ = layui.jquery,
      form = layui.form,

      treeTable = layui.treeTable;

    $.ajax({
      type: 'GET',
      url: local + 'module/list',
      dataType: "json",
      headers: {
        'Authorization': `Bearer ${ localStorage.getItem("token") }`,
      },
      contentType: 'application/json;charset=UTF-8',
      success: function (res) {
        treeTable.render({
          elem: '#currentTableId',
          data: res.data,
          tree: {
            customName: {
              name: 'moduleName',
              children: 'childList'
            },
            view: {
              iconLeaf: '<i class="layui-icon layui-icon-file"></i>',
            }
          },
          toolbar: '#toolbarDemo',
          cols: [[
            {field: 'moduleName', title: '名称'},
            {field: 'updateDate', title: '更新时间'},
            {fixed: "right", title: "操作", width: 190, align: "center", toolbar: "#currentTableBar"}
          ]],
          page: false,
          parseData: function (res) {
            return {
              "code": res.code === 200 ? 0 : res.code,
              "msg": res.message,
              // "count": res.data.total,
              "data": res.data.records
            };
          },
        });

        // form.val('demo-val-filter', {
        //   'orgName':res.data.orgName,
        //   'personCharge':res.data.personCharge,
        //   'tel':res.data.tel,
        //   'navFlag':res.data.navFlag,
        // });
        form.render('select')
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
      }
    });



    // table.render({
    //   elem: '#currentTableId',
    //   url: local + 'org/search/list',
    //   method: "post",
    //   contentType: 'application/json;charset=UTF-8',
    //   headers: {'Authorization': `Bearer ${ localStorage.getItem("token") }`},
    //   request: {
    //     pageName: 'pageNum',
    //     limitName: 'pageSize'
    //   },
    //   toolbar: '#toolbarDemo',
    //   defaultToolbar: ['filter', 'exports', 'print'],
    //   cols: [[
    //     {type: 'numbers', title: '序号', width: 60, fixed: 'left'},
    //     {field: 'orgName', title: '组织名称'},
    //     {field: 'personCharge', title: '负责人'},
    //     {field: 'tel', title: '联系电话'},
    //     {field: 'updateDate', title: '更新时间'},
    //     {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
    //   ]],
    //   limits: [10, 15, 20, 25, 50, 100],
    //   limit: 10,
    //   page: true,
    //   unresize: false,
    //   skin: 'line',
    //   parseData: function (res) {
    //     return {
    //       "code": res.code === 200 ? 0 : res.code,
    //       "msg": res.message,
    //       "count": res.data.total,
    //       "data": res.data.records
    //     };
    //   },
    // });

    /**
     * toolbar监听事件
     */
    treeTable.on('toolbar(currentTableFilter)', function (obj) {
      if (obj.event === 'add') {
        var index = layer.open({
          title: '添加角色',
          type: 2,
          shade: 0.2,
          maxmin: true,
          full: true,
          shadeClose: true,
          area: ['100%', '100%'],
          content: './add.html',
        });
        $(window).on("resize", function () {
          layer.full(index);
        });
      }
    });

    //监听表格复选框选择
    treeTable.on('checkbox(currentTableFilter)', function (obj) {
      console.log(obj)
    });

    treeTable.on('tool(currentTableFilter)', function (obj) {
      var data = obj.data;
      if (obj.event === 'edit') {
        var index = layer.open({
          title: '编辑账户',
          type: 2,
          shade: 0.2,
          maxmin: true,
          shadeClose: true,
          data: {id: '1234'},
          area: ['50%', '50%'],
          content: './edit.html?id=' + data.orgId,
        });
        $(window).on("resize", function () {
          layer.full(index);
        });
        return false;
      } else if (obj.event === 'delete') {
        $.ajax({
          type: 'POST',
          url: local + 'org/delete/' + data.orgId,
          data: {},
          dataType: "json",
          headers: {
            'Authorization': `Bearer ${ localStorage.getItem("token") }`,
          },
          contentType: 'application/json;charset=UTF-8',
          success: function (res) {
            if (res.code === 200) {
              layui.table.reload('currentTableId')
            } else {
              layer.msg(res.msg, {icon: 0});
            }
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
          }
        });
      }
    });

  });
</script>

</body>
</html>
